home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / FIXPATH.ICN < prev    next >
Text File  |  1992-11-30  |  2KB  |  59 lines

  1. ############################################################################
  2. #
  3. #    File:     fixpath.icn
  4. #
  5. #    Subject:  Program to replace path in a binary file
  6. #
  7. #    Author:   Gregg M. Townsend
  8. #
  9. #    Date:     July 6, 1990
  10. #
  11. ###########################################################################
  12. #
  13. #  Usage:  fixpath filename oldpath newpath
  14. #
  15. #  Fixpath changes file paths or other strings in a binary file by modifying
  16. #  the file in place.  Each null-terminated occurrence of "oldpath" is
  17. #  replaced by "newpath".
  18. #
  19. #  If the new path is longer than the old one, a warning is given and the
  20. #  old path is extended by null characters, which must be matched in the
  21. #  file for replacement to take place.  This is dangerous in general but
  22. #  allows repairing an errant fixpath command.
  23. #
  24. ############################################################################
  25.  
  26.  
  27. procedure main(args)
  28.   local fname, oldpath, newpath, f, pgm, n, p, s
  29.  
  30.    (*args == 3)    | stop("usage: fixpath filename oldpath newpath")
  31.    fname := args[1]
  32.    oldpath := args[2]
  33.    newpath := args[3]
  34.    if *newpath > *oldpath then {
  35.       write(&errout, "warning: newpath is longer than oldpath")
  36.       oldpath := left(oldpath, *newpath, "\0")
  37.    }
  38.    oldpath ||:= "\0"
  39.    newpath := left(newpath, *oldpath, "\0")
  40.  
  41.    (f := open(fname, "rwu"))        | stop(fname, ": can't open")
  42.    pgm := ""
  43.    while pgm ||:= reads(f, 8192)
  44.    (*pgm > 0)                | stop(fname, ": empty file")
  45.    n := 0
  46.    pgm ? {
  47.       while tab(p := find(oldpath)) do {
  48.          seek(f, p)            | stop(fname, ": can't seek")
  49.          writes(f, s, newpath)        | stop(fname, ": can't write")
  50.          move(*newpath)
  51.          n +:= 1
  52.       }
  53.       (n > 0) | stop(fname, ": can't find string `", args[2], "'")
  54.    }
  55.    write("replaced ", n, " occurrence", if n>1 then "s" else "")
  56.  
  57. end
  58.  
  59.